﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Task.Data;
using CentralOffice.Task.BusinessActions;

using ApplicationTable = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsDataTable;
using ApplicationRow = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsRow;

using ApprovalTable = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsDataTable;
using ApprovalRow = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsRow;

using CentralOffice.MyTable.Data;
using CentralOffice.MyTable.BusinessActions;
using Husb.Security;
using CentralOffice.Admin.BusinessActions;

using SmsTable = CentralOffice.MyTable.Data.SmsDataSet.smsDataTable;
using SmsRow = CentralOffice.MyTable.Data.SmsDataSet.smsRow;
using SmsBodyTable = CentralOffice.MyTable.Data.SmsDataSet.sms_bodyDataTable;
using SmsBodyRow = CentralOffice.MyTable.Data.SmsDataSet.sms_bodyRow;
using System.Collections;
using System.Data;



namespace CentralOffice.WebClient.Tasks
{
    public partial class ApplicationQuery : PageBase
    {
        private bool? authorized = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlCategory, SiteConstants.ApprovalObject, true);
                ddlCategory.SelectedIndex = 0;

                if (authorized == null)
                {
                    authorized = this.Authorize(AuthorizationRules.Application_Delete);
                }
                if (!authorized.Value)
                {
                    gv.Columns.RemoveAt(gv.Columns.Count - 1);
                }
            }
        }

        protected void ods_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            // OnRowDataBound="gv_RowDataBound"
            //  OnSelecting="odsApplication_Selecting"
            int status = EnumUtil.GetInt32(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, "通过");


            IOrderedDictionary parameters = e.InputParameters;
            ApplicationRow application = (new ApplicationDataSet()).Applications.NewApplicationsRow();
            application.Applicant = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
            //parameters["whereClause"] = task.FilterExpression;
            parameters["whereClause"] = application.FilterExpression;
            //parameters.Remove("Id");
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.PageIndex = 0;

            ApplicationRow application = (new ApplicationDataSet()).Applications.NewApplicationsRow();

            application.Name = txtName.Text;
            if (ddlCategory.SelectedIndex > 0 )
            {
                application.Category = Int32.Parse(ddlCategory.SelectedItem.Value);
            }
            application.Applicant = txtApplicant.Text;
            
            if (txtDateOfApplication.Text.Length > 0)
            {
                application.DateOfApplication = DateTime.Parse(txtDateOfApplication.Text);
            }
            if (txtDateOfApplicationEnd.Text.Length > 0)
            {
                application.DateOfApplicationEnd = DateTime.Parse(txtDateOfApplicationEnd.Text);
            }
            //task.CloseDate = txtCloseDate.Text;
            //task.Profile = txtProfile.Text;

            odsApplication.SelectParameters.Clear();
            odsApplication.SelectParameters.Add("whereClause", application.FilterExpression);
            //odsTask.SelectParameters["whereClause"].DefaultValue = task.FilterExpression;//companyInfo.FilterExpression

            //CentralOffice.Task.BusinessActions.Applications.DeleteById(new Guid("d6fefe93-e0e2-4b0f-b2f8-3522be7533e3"), true, true);
        }


        string applicant = null;
        string applicationTitle = null;
        protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete" || e.CommandName == "删除")
            {
                int index = Int32.Parse(e.CommandArgument.ToString());
                applicant = gv.Rows[index].Cells[2].Text;
                HyperLink a = gv.Rows[index].Cells[0].Controls[0] as HyperLink;
                applicationTitle = a.Text;
            }
            //gv.DataKeys[

            // onrowcommand="gv_RowCommand" onrowdeleted="gv_RowDeleted"
            // ondeleted="odsApplication_Deleted" 
        }
        protected void odsApplication_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {

            IDictionary parameters = e.OutputParameters;

            SmsDataSet smsBody = null;
            SmsBodyRow smsbodyrow = (new SmsDataSet()).sms_body.Newsms_bodyRow();

            smsbodyrow.FromId = ApplicationContext.ActualUserId.ToString();
            smsbodyrow.SmsType = "1";
            smsbodyrow.Content = "申请：" + applicationTitle + " 被删除。";
            smsbodyrow.SendTime = DateTime.Now;
            smsbodyrow.RemindUrl = "无";
            smsbodyrow.sGuid = Guid.NewGuid().ToString();
            smsbodyrow.Table.Rows.Add(smsbodyrow);
            smsBody = smsbodyrow.Table.DataSet as SmsDataSet;

            CentralOffice.MyTable.BusinessActions.SmsBody.Update(smsBody);

            smsbodyrow = CentralOffice.MyTable.BusinessActions.SmsBody.GetById(new Guid(smsbodyrow.sGuid));
            SmsDataSet smsds = new SmsDataSet();
            SmsRow smsrowTmp = smsds.sms.NewsmsRow();

            List<MembershipUserEx> employees = UserManager.GetAllUsersWithEmp(1);
            MembershipUserEx emp = employees.First(employee => employee.Name == applicant);

            smsrowTmp.ToId = emp == null ? ApplicationContext.ActualUserId.ToString() : emp.EmployeeId.ToString();
            smsrowTmp.RemindFlag = "0";
            smsrowTmp.DeleteFlag = "0";
            smsrowTmp.BodyId = smsbodyrow.BodyId;
            smsds.sms.Rows.Add(smsrowTmp);

            CentralOffice.MyTable.BusinessActions.Sms.Update(smsds);
        }
        protected void gv_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            IOrderedDictionary keys = e.Keys;
            IOrderedDictionary values = e.Values;

        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApplicationRow row = (e.Row.DataItem as DataRowView).Row as ApplicationRow;
                if (!row.IsApplicationCreatedByNull() && row.ApplicationCreatedBy != row.Applicant)
                {
                    //e.Row.Style.Add(HtmlTextWriterStyle.Color, "#483D8B");
                    foreach (TableCell cell in e.Row.Cells)
                    {
                        cell.Style.Add(HtmlTextWriterStyle.Color, "#483D8B");
                    }
                }

                TableCell statusCell = e.Row.Cells[6];
                if (statusCell.Text == "在审")
                {
                    statusCell.Style.Add(HtmlTextWriterStyle.Color, SiteUtil.GetExceptionalDataColor());
                }
                else if (statusCell.Text == "通过")
                {
                    statusCell.Style.Add(HtmlTextWriterStyle.Color, "#0f0");
                }
                else if (statusCell.Text == "否决")
                {
                    statusCell.Style.Add(HtmlTextWriterStyle.Color, "#f00");
                }
                else if (statusCell.Text == "已执行")
                {
                    statusCell.Style.Add(HtmlTextWriterStyle.Color, "#6f0");
                }

            }
        }
    }
}
